package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.NavDao;
import com.cn.wanxi.dao.impl.NavDaoImpl;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.model.ResultModel;
import com.cn.wanxi.service.NavService;
import com.cn.wanxi.util.Result;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

public class NavServiceImpl implements NavService {
    /**
     * 1.判断数据在redis
     * 2.如果有，则取数据封装到redis
     * 3.如果没有，则取数据库，同时存入redis
     * 4.无论新增，更新，删除，都应该删除所有集合的key值。
     *
     * @return
     */
    @Override
    public List<NavModel> getNavModelList() {
        NavDao navDao = new NavDaoImpl();
        List<NavModel> list = new ArrayList<>();
        Jedis jedis = new Jedis();
        jedis.select(10);
        Boolean key = jedis.exists("navHrefs");
        if (key) {
//从redis取出数据，封装成list集合
//            long length = jedis.llen("NAV:HREFS:ID:"+107640977);
            long length = jedis.llen("navHrefs");
            List<String> navHrefs = jedis.lrange("navHrefs", 0, -1);
            List<String> navTitles = jedis.lrange("navTitles", 0, -1);
            for (int i = 0; i < length; i++) {
                NavModel model = new NavModel();
                model.setHref(navHrefs.get(i));
                model.setTitle(navTitles.get(i));
                list.add(model);
            }
            System.out.println("redis");
        } else {
            list = navDao.getNavModelList();
//            存入redis
//            String[] navHrefs = new String[list.size()];
//            String[] navTitles = new String[list.size()];
//            for (int i = 0; i < list.size(); i++) {
//                navHrefs[i] = list.get(i).getHref();
//                navTitles[i] = list.get(i).getTitle();
//            }
//            jedis.rpush("navHrefs", navHrefs);
//            jedis.rpush("navTitles", navTitles);
            for (int i = 0; i < list.size(); i++) {
                jedis.rpush("navHrefs", list.get(i).getHref());
                jedis.rpush("navTitles", list.get(i).getTitle());
            }
            System.out.println("mysql");
        }
        return list;
    }

    public static void main(String[] args) {
//        List<NavModel> list = new ArrayList<>();
//        for (int i = 0; i < 4; i++) {
//            NavModel model = new NavModel();
//            model.setTitle(i + "");
//            model.setHref(i + "");
//            list.add(model);
//        }
//        System.out.println(list);

    }

    @Override
    public ResultModel add(NavModel navModel) {
        return null;
    }

    @Override
    public ResultModel update(NavModel navModel) {
        NavDao navDao = new NavDaoImpl();
        int count = navDao.update(navModel);
        if (count > 0) {
//            删除key
            Jedis jedis = new Jedis();
            jedis.del("navHrefs", "navTitles");
        }
        return ResultModel.getResultModel("", Result.SUCCESS_CODE, count, null);
    }

    @Override
    public ResultModel delete(NavModel navModel) {
        return null;
    }

    @Override
    public ResultModel findAll(NavModel navModel) {
        return null;
    }

    @Override
    public ResultModel findById(NavModel navModel) {
        return null;
    }

    @Override
    public ResultModel getCount(NavModel navModel) {
        return null;
    }

    @Override
    public ResultModel changeEnable(NavModel navModel) {
        return null;
    }
}
